package com.weme.common.controller

import android.content.Context
import android.util.Log
import android.view.*
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.weme.common.R
import com.weme.common.databinding.LayoutFloationLiveWindowBinding
import com.weme.common.util.displayMetrics
import java.lang.ref.WeakReference

object FloatingWindowController {

    private var contextRef: WeakReference<Context>? = null

    fun initialize(context: Context) {
        contextRef = WeakReference(context)
    }

    fun showFloatingWindow(viewGroup: ViewGroup,surfaceView : SurfaceView) {
        val context = contextRef?.get() ?: return
        val binding = LayoutFloationLiveWindowBinding.inflate(LayoutInflater.from(context))
        val layoutParams = ConstraintLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        ).apply {
            topToTop = ConstraintLayout.LayoutParams.PARENT_ID
            startToStart = ConstraintLayout.LayoutParams.PARENT_ID
            marginStart = context.displayMetrics.widthPixels - context.resources.getDimension(com.example.resources.R.dimen.dp_120).toInt()
            topMargin = 300
        }

        viewGroup.addView(binding.root, layoutParams)
        enableDrag(binding.root)

        binding.localVideoViewContainer.addView(surfaceView)
    }

    private fun enableDrag(view: View) {
        view.setOnTouchListener(object : View.OnTouchListener {
            private var initialX = 0
            private var initialY = 0
            private var initialTouchX = 0f
            private var initialTouchY = 0f

            override fun onTouch(v: View, event: MotionEvent): Boolean {
                val layoutParams = v.layoutParams as ConstraintLayout.LayoutParams
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        initialX = layoutParams.leftMargin
                        initialY = layoutParams.topMargin
                        initialTouchX = event.rawX
                        initialTouchY = event.rawY
                        Log.d("FloatingWindow", "ACTION_DOWN: initialX=$initialX, initialY=$initialY, initialTouchX=$initialTouchX, initialTouchY=$initialTouchY")
                        return true
                    }
                    MotionEvent.ACTION_MOVE -> {
                        layoutParams.leftMargin = initialX + (event.rawX - initialTouchX).toInt()
                        layoutParams.topMargin = initialY + (event.rawY - initialTouchY).toInt()
                        v.layoutParams = layoutParams
                        v.requestLayout()  // 强制视图重新布局
                        Log.d("FloatingWindow", "ACTION_MOVE: leftMargin=${layoutParams.leftMargin}, topMargin=${layoutParams.topMargin}")
                        return true
                    }
                }
                return false
            }
        })
    }

    fun hideFloatingWindow(viewGroup: ViewGroup) {
        // Implementation to hide the floating window
        // This might include removing the view from the viewGroup
    }

    fun releaseResources() {
        contextRef?.clear()
    }
}
